import { useBaseSelectUserStoreCurrentToggleRole, useBaseSelectUserStoreFilterForm } from '@src/component/common/BaseSelectUser/store';
/* model */
import { 
  BaseSelectUserErrorMessageEnum, 
  BaseSelectUserWarnMessageEnum, 
  SelectUserComponentRole, 
  SelectUserComponentUser 
} from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* store */
import {
  useBaseSelectUserStoreFetchRoleList,
  useBaseSelectUserStoreFetchRoleUserList,
  useBaseSelectUserStoreLoadingMap,
  useBaseSelectUserStoreRoleList,
  useBaseSelectUserStoreUserCountMap,
  useBaseSelectUserStoreUserListMap,
} from '@src/component/common/BaseSelectUser/store'
/* hooks */
import { useBaseSelectUserRoleListParams, useBaseSelectUserRoleUserListParams } from '@src/component/common/BaseSelectUser/hooks/params'
import { useBaseSelectUserOptions } from '@src/component/common/BaseSelectUser/hooks/options';
/* util */
import Log from '@src/util/log'
import { message } from '@src/util/message'
import { isDisabledSelectItem, setBaseSelectUserListChecked, transformRoleList } from '@src/component/common/BaseSelectUser/util'
import { isFalse } from '@src/util/type'
/* vue */
import { computed } from 'vue'

function useSelectUserRoleUserList() {
  
  const { roleUserList } = useBaseSelectUserStoreUserListMap()
  const { roleUserCount } = useBaseSelectUserStoreUserCountMap()
  
  // 角色人员列表是否为空
  const isUserListEmpty = computed(() => {
    return roleUserList.value.length == 0 || roleUserCount.value == 0
  })
  
  // 角色人员列表是否全部加载完成
  const isUserListAllLoaded = computed(() => {
    return roleUserList.value.length >= roleUserCount.value
  })
  
  // 角色人员列表是否未全部加载完成
  const isUserListNotAllLoaded = computed(() => {
    return !isUserListAllLoaded.value
  })
  
  // 是否禁用加载更多人员状态
  const isLoadmoreDisabled = computed(() => {
    
    // 当前人员数量为空时，不禁用加载状态可以加载更多
    if (isUserListEmpty.value) {
      return false
    }
    
    // 全部加载人员后，禁用加载
    return isUserListAllLoaded.value
    
  })
  
  const isLoadmoreNotDisabled = computed(() => isFalse(isLoadmoreDisabled.value))
  
  return {
    isUserListEmpty,
    isUserListAllLoaded,
    isLoadmoreDisabled,
    isLoadmoreNotDisabled,
    isUserListNotAllLoaded
  }
  
}

/** 
 * @description 获取选人组件 角色列表
*/
function useFetchRoleList() {
  
  const { setRoleList } = useBaseSelectUserStoreRoleList()
  const { setRoleLoading, clearRoleLoading } = useBaseSelectUserStoreLoadingMap()
  const { roleListParams } = useBaseSelectUserRoleListParams()
  const { isOnlyShowBusinessServiceCloudRole } = useBaseSelectUserOptions()
  
  const { isFilterRoleFormRoleNameNotEmpty } = useBaseSelectUserStoreFilterForm()
  
  const fetchRoleList = useBaseSelectUserStoreFetchRoleList()
  
  const fetchRoleListImpl = () => {
    
    setRoleLoading()
    
    return (
      fetchRoleList(roleListParams.value).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          let roleList = (result?.data || []) as SelectUserComponentRole[]
          
          if (isFilterRoleFormRoleNameNotEmpty.value) {
            roleList = roleList.filter(role => isFalse(isDisabledSelectItem(role)))
          }
          
          // 处理产品线的角色列表
          const options = {
            isOnlyShowBusinessServiceCloudRole: isOnlyShowBusinessServiceCloudRole.value
          }
          roleList = transformRoleList(roleList, options)
          
          setBaseSelectUserListChecked(roleList)
          
          setRoleList(roleList)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.RoleList)
        }
        
      }).finally(() => {
        clearRoleLoading()
      })
    )
  }
  
  return fetchRoleListImpl
}

/** 
 * @description 获取选人组件 角色下 人员列表
*/
function useFetchRoleUserList() {
  
  const { roleUserList, setRoleUserList } = useBaseSelectUserStoreUserListMap()
  const { setRoleUserCount } = useBaseSelectUserStoreUserCountMap()
  const { setRoleUserLoading, clearRoleUserLoading } = useBaseSelectUserStoreLoadingMap()
  const { isUserListNotAllLoaded, isLoadmoreDisabled } = useSelectUserRoleUserList()
  const { roleUserListParams } = useBaseSelectUserRoleUserListParams()
  
  const fetchRoleUserList = useBaseSelectUserStoreFetchRoleUserList()
  
  const fetchRoleUserListImpl = (isInit: boolean = false) => {
    
    if (isLoadmoreDisabled.value && isFalse(isInit)) {
      Log.warn(BaseSelectUserWarnMessageEnum.RoleUserList, fetchRoleUserListImpl.name)
      return Promise.resolve()
    }
    
    setRoleUserLoading()
    
    return (
      fetchRoleUserList(roleUserListParams.value).then(result => {
        
        if (MsgModel.isSuccess(result)) {
          
          const userCount = result?.data?.userTotalCount || 0
          let userList = (result?.data?.userList || []) as SelectUserComponentUser[]
          
          // 角色人员列表没有全部加载完成，则合并新的数据
          if (isUserListNotAllLoaded.value) {
            userList = roleUserList.value.concat(userList)
          }
          
          setBaseSelectUserListChecked(userList)
          
          setRoleUserList(userList)
          setRoleUserCount(userCount)
          
        } else {
          message.error(result?.message || BaseSelectUserErrorMessageEnum.RoleUserList)
        }
        
      }).finally(() => {
        clearRoleUserLoading()
      })
    )
  }
  
  return fetchRoleUserListImpl
}

function useFetchRoleListAndRoleUserList() {
  
  const fetchRoleList = useFetchRoleList()
  const fetchRoleUserList = useFetchRoleUserList()
  
  const { roleList, setRoleList } = useBaseSelectUserStoreRoleList()
  const { setCurrentToggleRole } = useBaseSelectUserStoreCurrentToggleRole()
  const { resetPageNum } = useBaseSelectUserRoleUserListParams()
  const { setRoleUserCount } = useBaseSelectUserStoreUserCountMap()
  const { setRoleUserList } = useBaseSelectUserStoreUserListMap()
  const { clearRoleUserLoading } = useBaseSelectUserStoreLoadingMap()
  
  const fetchRoleListAndRoleUserList = (isResetCurrentRoleNav: boolean = true) => {
    
    setRoleList()
    
    fetchRoleList().then(() => {
      
      const firstRole = roleList.value[0]
      const isDisabled = isDisabledSelectItem(firstRole)
      const isNotDisabled = isFalse(isDisabled)
      
      setRoleUserCount()
      setRoleUserList()
      
      if (isDisabled) {
        return
      }
      
      if (firstRole && isNotDisabled) {
        
        if (isResetCurrentRoleNav) {
          setCurrentToggleRole(firstRole)
        }
        
        resetPageNum()
        setRoleUserCount()
        fetchRoleUserList()
        
      } else {
        clearRoleUserLoading()
      }
      
    })
    
  }
  
  return fetchRoleListAndRoleUserList
}

export {
  useSelectUserRoleUserList,
  useFetchRoleList,
  useFetchRoleUserList,
  useFetchRoleListAndRoleUserList
}